﻿using System;
using CH.FourBoards.LagerWalti.Persistence.Entities;
using CH.FourBoards.LagerWalti.Persistence.Setup.Creation;

namespace CH.FourBoards.LagerWalti.Persistence.Setup.FixedData
{
    internal class SampleDataGenerator : IDataGenerator
    {
        private Category _wheelsCategory;
        private Category _completePackagesCategory;
        private Product _edgeWheels;
        private Product _g3Pro;
        private Product _g3DownHill;

        public void Create(DataContext context)
        {
            CreateCategories(context);
            CreateProducts(context);
            CreateStock(context);
        }

        private void CreateStock(DataContext context)
        {
            context.Transactions.Add(AddStock(_edgeWheels, 14));
            context.Transactions.Add(AddStock(_g3Pro, 4));
            context.Transactions.Add(AddStock(_g3DownHill, 0));
        }

        private void CreateProducts(DataContext context)
        {
            _edgeWheels = new Product {
                CategoryId = _wheelsCategory.Id,
                Name = "daBlues Edge Wheels",
                BundleSize = 4
            };
            context.Products.Add(_edgeWheels);
            _g3Pro = new Product {
                CategoryId = _completePackagesCategory.Id,
                Name = "G3 Pro Complate Package",
                BundleSize = 1
            };
            context.Products.Add(_g3Pro);
            _g3DownHill = new Product {
                CategoryId = _completePackagesCategory.Id,
                Name = "G3 DownHill Complate Package",
                BundleSize = 1
            };
            context.Products.Add(_g3DownHill);
            context.SaveChanges();
        }

        private void CreateCategories(DataContext context)
        {
            // create Categories
            _wheelsCategory = new Category {
                Name = "Wheels",
                Image = "res/images/wheels.jpg"
            };
            context.Categories.Add(_wheelsCategory);
            _completePackagesCategory = new Category {
                Name = "Complate Packages",
                Image = "res/images/complete-packages.jpg"
            };
            context.Categories.Add(_completePackagesCategory);
            context.SaveChanges();
        }

        private Transaction AddStock(Product product, int quantity)
        {
            return new Transaction {
                Date = DateTime.Now,
                ProductId = product.Id,
                TypeId = 1,
                Quantity = quantity
            };
        }
    }
}
